﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MachineVision.Extensions
{
    public static class LanguageHelper
    {
        public static string AppCurrentLanguage { get; set; }
        public static Dictionary<string, string> KeyValues { get; set; } = new Dictionary<string, string>();
        /// <summary>
        /// 根据资源文件在应用程序资源中的位置实现切换
        /// </summary>
        /// <param name="key"></param>
        public static void SetLanguage(string key)
        {
            var res = App.Current.Resources.MergedDictionaries.FirstOrDefault(x =>
            x.Source != null &&
            x.Source.OriginalString != null &&
            x.Source.OriginalString.Contains(key)
            );

            if (res != null)
                App.Current.Resources.MergedDictionaries.Remove(res);
            {
                App.Current.Resources.MergedDictionaries.Add(res);
                AppCurrentLanguage = key;
                KeyValues.Clear();
                foreach (DictionaryEntry item in res)
                    KeyValues.Add(item.Key?.ToString(), item.Value?.ToString());
                //区域性设置
                Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(key);
            }
        }
    }
}
